#region Copyright (c) 2006-2009 Widgetsphere LLC, All Rights Reserved
//--------------------------------------------------------------------- *
//                          Widgetsphere  LLC                           *
//             Copyright (c) 2006-2009 All Rights reserved              *
//                                                                      *
//                                                                      *
//This file and its contents are protected by United States and         *
//International copyright laws.  Unauthorized reproduction and/or       *
//distribution of all or any portion of the code contained herein       *
//is strictly prohibited and will result in severe civil and criminal   *
//penalties.  Any violations of this copyright will be prosecuted       *
//to the fullest extent possible under law.                             *
//                                                                      *
//THE SOURCE CODE CONTAINED HEREIN AND IN RELATED FILES IS PROVIDED     *
//TO THE REGISTERED DEVELOPER FOR THE PURPOSES OF EDUCATION AND         *
//TROUBLESHOOTING. UNDER NO CIRCUMSTANCES MAY ANY PORTION OF THE SOURCE *
//CODE BE DISTRIBUTED, DISCLOSED OR OTHERWISE MADE AVAILABLE TO ANY     *
//THIRD PARTY WITHOUT THE EXPRESS WRITTEN CONSENT OF WIDGETSPHERE LLC   *
//                                                                      *
//UNDER NO CIRCUMSTANCES MAY THE SOURCE CODE BE USED IN WHOLE OR IN     *
//PART, AS THE BASIS FOR CREATING A PRODUCT THAT PROVIDES THE SAME, OR  *
//SUBSTANTIALLY THE SAME, FUNCTIONALITY AS ANY WIDGETSPHERE PRODUCT.    *
//                                                                      *
//THE REGISTERED DEVELOPER ACKNOWLEDGES THAT THIS SOURCE CODE           *
//CONTAINS VALUABLE AND PROPRIETARY TRADE SECRETS OF WIDGETSPHERE,      *
//INC.  THE REGISTERED DEVELOPER AGREES TO EXPEND EVERY EFFORT TO       *
//INSURE ITS CONFIDENTIALITY.                                           *
//                                                                      *
//THE END USER LICENSE AGREEMENT (EULA) ACCOMPANYING THE PRODUCT        *
//PERMITS THE REGISTERED DEVELOPER TO REDISTRIBUTE THE PRODUCT IN       *
//EXECUTABLE FORM ONLY IN SUPPORT OF APPLICATIONS WRITTEN USING         *
//THE PRODUCT.  IT DOES NOT PROVIDE ANY RIGHTS REGARDING THE            *
//SOURCE CODE CONTAINED HEREIN.                                         *
//                                                                      *
//THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE.              *
//--------------------------------------------------------------------- *
#endregion
using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Widgetsphere.Generator.Models;
using Widgetsphere.Generator.Common.GeneratorFramework;

namespace Widgetsphere.Generator.Forms
{
	internal class RowEntryCollectionForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button cmdOK;
		private System.Windows.Forms.Button cmdCancel;
    private Label label1;
    private DataGridView dataGridView1;
    private DataGridViewTextBoxColumn Column1;
    private DataGridViewTextBoxColumn Column2;
    private Button cmdPaste;
		private Button cmdCopy;
		private System.ComponentModel.Container components = null;

		public RowEntryCollectionForm()
		{
			InitializeComponent();
		}

    public RowEntryCollectionForm(Table table)
      : this()
    {
      _table = table;

      this.dataGridView1.Columns.Clear();

      //Setup Columns
      System.Data.DataTable dt = table.CreateDataTable();
      int columnCount = dt.Columns.Count;      

      //Add Data
      foreach (RowEntry rowEntry in table.StaticData)
      {
        //Create a row
        System.Data.DataRow dr = dt.NewRow();
        dt.Rows.Add(dr);

        //Loop through the items and create datarows
        int count = System.Math.Min(rowEntry.CellEntries.Count, columnCount);
        for (int ii = 0; ii < count; ii++)
        {
          CellEntry cellEntry = rowEntry.CellEntries[ii];
          dr[ii] = cellEntry.Value;
        }
      }

      //Bind the grid
      this.dataGridView1.DataSource = dt;

    }

    protected override void Dispose(bool disposing)
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.cmdOK = new System.Windows.Forms.Button();
			this.cmdCancel = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.dataGridView1 = new System.Windows.Forms.DataGridView();
			this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.cmdPaste = new System.Windows.Forms.Button();
			this.cmdCopy = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
			this.SuspendLayout();
			// 
			// cmdOK
			// 
			this.cmdOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.cmdOK.Location = new System.Drawing.Point(473, 232);
			this.cmdOK.Name = "cmdOK";
			this.cmdOK.Size = new System.Drawing.Size(80, 24);
			this.cmdOK.TabIndex = 1;
			this.cmdOK.Text = "OK";
			this.cmdOK.Click += new System.EventHandler(this.cmdOK_Click);
			// 
			// cmdCancel
			// 
			this.cmdCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.cmdCancel.Location = new System.Drawing.Point(561, 232);
			this.cmdCancel.Name = "cmdCancel";
			this.cmdCancel.Size = new System.Drawing.Size(80, 24);
			this.cmdCancel.TabIndex = 2;
			this.cmdCancel.Text = "Cancel";
			this.cmdCancel.Click += new System.EventHandler(this.cmdCancel_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(12, 9);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(392, 210);
			this.label1.TabIndex = 2;
			this.label1.Text = "The interface for this functionality has not been implemented.";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// dataGridView1
			// 
			this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
									| System.Windows.Forms.AnchorStyles.Left)
									| System.Windows.Forms.AnchorStyles.Right)));
			this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.Column1,
            this.Column2});
			this.dataGridView1.Location = new System.Drawing.Point(12, 9);
			this.dataGridView1.Name = "dataGridView1";
			this.dataGridView1.RowTemplate.Height = 20;
			this.dataGridView1.Size = new System.Drawing.Size(621, 210);
			this.dataGridView1.TabIndex = 0;
			// 
			// Column1
			// 
			this.Column1.HeaderText = "Column1";
			this.Column1.Name = "Column1";
			// 
			// Column2
			// 
			this.Column2.HeaderText = "Column2";
			this.Column2.Name = "Column2";
			// 
			// cmdPaste
			// 
			this.cmdPaste.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.cmdPaste.Location = new System.Drawing.Point(104, 232);
			this.cmdPaste.Name = "cmdPaste";
			this.cmdPaste.Size = new System.Drawing.Size(80, 24);
			this.cmdPaste.TabIndex = 1;
			this.cmdPaste.Text = "Paste";
			this.cmdPaste.Click += new System.EventHandler(this.cmdPaste_Click);
			// 
			// cmdCopy
			// 
			this.cmdCopy.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.cmdCopy.Location = new System.Drawing.Point(16, 232);
			this.cmdCopy.Name = "cmdCopy";
			this.cmdCopy.Size = new System.Drawing.Size(80, 24);
			this.cmdCopy.TabIndex = 3;
			this.cmdCopy.Text = "Copy";
			this.cmdCopy.Click += new System.EventHandler(this.cmdCopy_Click);
			// 
			// RowEntryCollectionForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(645, 261);
			this.Controls.Add(this.cmdCopy);
			this.Controls.Add(this.dataGridView1);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.cmdCancel);
			this.Controls.Add(this.cmdPaste);
			this.Controls.Add(this.cmdOK);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.MinimumSize = new System.Drawing.Size(416, 288);
			this.Name = "RowEntryCollectionForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Static Data";
			((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

    #region Class Members

    protected Table _table = null;

    #endregion

    #region Property Implementations

    public Table Table
    {
      get { return _table; }
    }

    #endregion

    #region Button Handlers

    private void cmdOK_Click(object sender, System.EventArgs e)
		{
      this.Table.StaticData.Clear();
      System.Data.DataTable dt = (System.Data.DataTable)this.dataGridView1.DataSource;
      foreach (System.Data.DataRow dr in dt.Rows)
      {
        RowEntry rowEntry = new RowEntry(this.Table.Root);
        for(int ii=0;ii<this.Table.Columns.Count;ii++)
        {
          CellEntry cellEntry = new CellEntry(this.Table.Root);
          cellEntry.ColumnRef = this.Table.Columns[ii];
          cellEntry.Value = dr[ii].ToString();
          rowEntry.CellEntries.Add(cellEntry);
        }        
        this.Table.StaticData.Add(rowEntry);
      }

			this.DialogResult = DialogResult.OK;
			this.Close();
		}

		private void cmdCancel_Click(object sender, System.EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

    private void cmdPaste_Click(object sender, EventArgs e)
    {
      string text = Clipboard.GetText();
      string[] lines = text.Split('\n');
      if(lines.Length <= 1)
        return;

      int columnCount = lines[0].Split('\t').Length;
      if(dataGridView1.ColumnCount != columnCount)
        return;

      DataTable table = (DataTable)this.dataGridView1.DataSource;
      for(int ii = 1 ; ii < lines.Length ;ii++ )
      {
        string line = lines[ii].Replace("\r", "");
        string[] columns = line.Split('\t');

        DataRow newRow = table.NewRow();
        for(int jj = 0 ; jj < columns.Length ;jj++)
        {
          newRow[jj] = columns[jj];
        }
        table.Rows.Add(newRow);
      }

    }

		private void cmdCopy_Click(object sender, EventArgs e)
		{
			string text = "";
			DataTable table = (DataTable)this.dataGridView1.DataSource;
			int jj = 0;
			foreach (DataRow dr in table.Rows)
			{
				for (int ii = 0; ii < table.Columns.Count; ii++)
				{
					text += dr[ii];
					if (ii < table.Columns.Count - 1)
						text += "\t";
				}

				if (jj < table.Rows.Count - 1)
					text += "\n";

				jj++;
			}
			Clipboard.SetText(text);
		}

		#endregion

	}
}
